package com.example.gzysmartcity.ui.fragment.factory;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentCarbuyBinding;
import com.example.gzysmartcity.repository.model.MyCarBuy;
import com.example.gzysmartcity.ui.adapter.CarBuyRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.CarBuyViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CarBuyFragment extends BaseFragment<FragmentCarbuyBinding> {
    private List<String> spnnerList;
    private static String carName="";
    @Override
    protected FragmentCarbuyBinding viewBinding() {
        return FragmentCarbuyBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        CarBuyViewModel carBuyViewModel=new ViewModelProvider(this).get(CarBuyViewModel.class);
        spnnerList=new ArrayList<>();
        spnnerList.add("请选择汽车名称");
        carBuyViewModel.getCarTypeData();
        carBuyViewModel.getcarBuyLiveData().observe(getViewLifecycleOwner(),state -> {
            switch (state){
                case SUCCESS:
                    List<MyCarBuy> myCarBuyList=carBuyViewModel.getCarBuyList();
                    List<MyCarBuy> myCarBuys=new ArrayList<>();
                    myCarBuys.addAll(myCarBuyList);
                    List<MyCarBuy> myCarBuyType=carBuyViewModel.getCarBuyList();
                    for (MyCarBuy dataBean: myCarBuyType){
                        spnnerList.add(dataBean.getCarTypeName());
                    }
                    ArrayAdapter<String> adapterspanner=new ArrayAdapter<String>(getContext(), R.layout.item_spanner,spnnerList);
                    b.spinnerCvarbus.setAdapter(adapterspanner);
                    CarBuyRvAdapter adapter=new CarBuyRvAdapter(R.layout.rv_buy_car);
                    adapter.setNewInstance(myCarBuys);
                    b.rvBuyCar.setAdapter(adapter);
                    b.rvBuyCar.setLayoutManager(new LinearLayoutManager(getContext()));
                    b.spinnerCvarbus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            if(position!=0){
                                carName=spnnerList.get(position);
                            }

                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });
                    b.btnSearchCarbus.setOnClickListener(v->{
                        if(carName.equals("")){
                            ToastUtils.showShort("汽车车型不能为空");
                        }else if(b.editCarbus.getText().toString().trim().equals("")){
                            //只查询车型
                            ToastUtils.showShort(carName);
                            myCarBuys.clear();
                            myCarBuys.addAll(carBuyViewModel.getSingSreach(carName));
                            adapter.notifyDataSetChanged();
                        }else {
                            myCarBuys.clear();
                            myCarBuys.addAll(carBuyViewModel.getSingPriceSreach(carName,b.editCarbus.getText().toString().trim()));
                            adapter.notifyDataSetChanged();
                        }
                    });
                    break;
                case FAIL:
                    break;
            }
        });
    }

    @Override
    protected String setTitle() {
        return "汽车销售";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }
}
